package com.chujian.system.core.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chujian.framework.bean.IdName;
import com.chujian.framework.bean.IdNameCompose;
import com.chujian.framework.core.base.BaseController;
import com.chujian.framework.core.base.BaseService;
import com.chujian.framework.core.tsdata.Result;
import com.chujian.core.entity.CoreDict;
import com.chujian.core.vo.CreDictVo;
import com.chujian.system.core.service.CoreDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 字典表控制层
 *
 * @author chujian
 * @since 2021-07-28
 */
@RequestMapping("/coreDict")
@RestController
@Slf4j
public class CoreDictController extends BaseController<CoreDict> {

    @Resource
    private CoreDictService baseService;

    @Override
    public BaseService<CoreDict> getBaseService() {
        return baseService;
    }

    /**
     * 获取父级字典
     *
     * @param current  current
     * @param size     size
     * @param coreDict coreDict
     * @return -
     */
    @GetMapping("/pageParent")
    public Result<?> pageParent(@RequestParam("current") Integer current,
                                @RequestParam("size") Integer size,
                                CoreDict coreDict) {
        IPage<CoreDict> page = baseService.pageParent(current, size, coreDict);
        return okRs(page);
    }

    /**
     * 获取字典项
     *
     * @param dictPid dictPid
     * @return -
     */
    @GetMapping("/items")
    public Result<?> getDictItems(@RequestParam("dictPid") Long dictPid) {
        List<CoreDict> list = baseService.listByPid(dictPid);
        return okRs(list);
    }

    /**
     * 获取字典项，仅包含ID和NAME
     *
     * @param dictPid dictPid
     * @return -
     */
    @GetMapping("/sampleItems")
    public Result<?> getDictSampleItems(@RequestParam("dictPid") Long dictPid) {
        List<IdName> items = baseService.listByPid(dictPid)
                .stream()
                .map(e -> new IdName(e.getId(), e.getName()))
                .collect(Collectors.toList());
        return okRs(items);
    }

    /**
     * 树形结构字典
     *
     * @param dictPid dictPid
     * @return -
     */
    @GetMapping("/treeItems")
    public Result<?> getTreeItems(@RequestParam("dictPid") Long dictPid) {
        List<CoreDict> items = baseService.getTreeItems(dictPid);
        return okRs(items);
    }

    /**
     * 树形结构字典，仅包含简单信息
     *
     * @param dictPid dictPid
     * @return -
     */
    @GetMapping("/sampleTreeItems")
    public Result<?> getSampleTreeItems(@RequestParam("dictPid") Long dictPid) {
        List<IdNameCompose> items = baseService.getSampleTreeItems(dictPid);
        return okRs(items);
    }

    /**
     * pageParentTrees，分页查询父级字典，树结构返回
     *
     * @param current  current
     * @param size     size
     * @param coreDict coreDict
     * @return -
     */
    @GetMapping("/pageParentTrees")
    public Result<?> pageParentTrees(@RequestParam("current") Integer current,
                                     @RequestParam("size") Integer size,
                                     CoreDict coreDict) {
        IPage<CoreDict> page = baseService.pageParentTrees(current, size, coreDict);
        return okRs(page);
    }

    /**
     * 新增字典
     *
     * @param creDictVo creDictVo
     * @return 操作成功
     */
    @PostMapping("/creDict")
    public Result<?> creDict(@RequestBody CreDictVo creDictVo) {
        baseService.creDict(creDictVo);
        return okRs();
    }

    /**
     * 添加字典项
     *
     * @param creDictVo creDictVo
     * @return 操作成功
     */
    @PostMapping("/addDictItems")
    public Result<?> addDictItems(@RequestBody CreDictVo creDictVo) {
        baseService.addDictItems(creDictVo);
        return okRs();
    }

}
